home *** CD-ROM | disk | FTP | other *** search
/ Freelog 125 / Freelog_MarsAvril2015_No125.iso / Musique / Quod Libet / quodlibet-3.3.0-installer.exe / bin / quodlibet / library / librarians.pyc (.txt) < prev    next >
Python Compiled Bytecode  |  2014-12-31  |  8KB  |  238 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.7)
  3.  
  4. '''
  5. Librarians for libraries.
  6. '''
  7. import itertools
  8. from gi.repository import GObject
  9. from quodlibet.util.dprint import print_d
  10.  
  11. class Librarian(GObject.GObject):
  12.     '''The librarian is a nice interface to all active libraries.
  13.  
  14.     Librarians are a kind of meta-library. When any of their
  15.     registered libraries fire a signal, they fire the same
  16.     signal. Likewise, they provide various methods equivalent to the
  17.     ones found in libraries that group the results of the real
  18.     libraries.
  19.  
  20.     Attributes:
  21.     libraries -- a dict mapping library names to libraries
  22.     '''
  23.     __gsignals__ = {
  24.         'changed': (GObject.SignalFlags.RUN_LAST, None, (object,)),
  25.         'removed': (GObject.SignalFlags.RUN_LAST, None, (object,)),
  26.         'added': (GObject.SignalFlags.RUN_LAST, None, (object,)) }
  27.     
  28.     def __init__(self):
  29.         super(Librarian, self).__init__()
  30.         self.libraries = { }
  31.         self._Librarian__signals = { }
  32.  
  33.     
  34.     def destroy(self):
  35.         pass
  36.  
  37.     
  38.     def register(self, library, name):
  39.         '''Register a library with this librarian.'''
  40.         if name in self.libraries or name in self._Librarian__signals:
  41.             raise ValueError('library %r is already active' % name)
  42.         added_sig = library.connect('added', self._Librarian__added)
  43.         removed_sig = library.connect('removed', self._Librarian__removed)
  44.         changed_sig = library.connect('changed', self._Librarian__changed)
  45.         self.libraries[name] = library
  46.         self._Librarian__signals[library] = [
  47.             added_sig,
  48.             removed_sig,
  49.             changed_sig]
  50.  
  51.     
  52.     def _unregister(self, library, name):
  53.         del self.libraries[name]
  54.         for signal_id in self._Librarian__signals[library]:
  55.             library.disconnect(signal_id)
  56.         
  57.         del self._Librarian__signals[library]
  58.  
  59.     
  60.     def __changed(self, library, items):
  61.         self.emit('changed', items)
  62.  
  63.     
  64.     def __added(self, library, items):
  65.         self.emit('added', items)
  66.  
  67.     
  68.     def __removed(self, library, items):
  69.         self.emit('removed', items)
  70.  
  71.     
  72.     def changed(self, items):
  73.         '''Triage the items and inform their real libraries.'''
  74.         for in_library in self.libraries.itervalues():
  75.             library = None
  76.             if in_library:
  77.                 library._changed(in_library)
  78.                 continue
  79.  
  80.     
  81.     def __getitem__(self, key):
  82.         '''Find a item given its key.'''
  83.         for library in self.libraries.itervalues():
  84.             
  85.             try:
  86.                 return library[key]
  87.             continue
  88.             except KeyError:
  89.                 continue
  90.             
  91.  
  92.         else:
  93.             raise KeyError(key)
  94.  
  95.     
  96.     def get(self, key, default = None):
  97.         
  98.         try:
  99.             return self[key]
  100.         except KeyError:
  101.             return default
  102.  
  103.  
  104.     
  105.     def remove(self, items):
  106.         '''Remove items from all libraries.'''
  107.         for library in self.libraries.itervalues():
  108.             library.remove(items)
  109.         
  110.  
  111.     
  112.     def __contains__(self, item):
  113.         '''Check if a key or item is in the library.'''
  114.         for library in self.libraries.itervalues():
  115.             if item in library:
  116.                 return True
  117.         else:
  118.             return False
  119.  
  120.     
  121.     def __iter__(self):
  122.         '''Iterate over all items in all libraries.'''
  123.         return itertools.chain(*self.libraries.itervalues())
  124.  
  125.     
  126.     def move(self, items, from_, to):
  127.         """Move items from one library to another.
  128.  
  129.         This causes 'removed' signals on the from library, and 'added'
  130.         signals on the 'to' library, but will not cause any signals
  131.         to be emitted via this librarian.
  132.         """
  133.         
  134.         try:
  135.             from_.handler_block(self._Librarian__signals[from_][1])
  136.             to.handler_block(self._Librarian__signals[to][0])
  137.             from_.remove(items)
  138.             to.add(items)
  139.         finally:
  140.             from_.handler_unblock(self._Librarian__signals[from_][1])
  141.             to.handler_unblock(self._Librarian__signals[to][0])
  142.  
  143.  
  144.  
  145.  
  146. class SongLibrarian(Librarian):
  147.     '''A librarian for SongLibraries.'''
  148.     
  149.     def tag_values(self, tag):
  150.         '''Return a list of all values for the given tag.'''
  151.         tags = set()
  152.         for library in self.libraries.itervalues():
  153.             tags.update(library.tag_values(tag))
  154.         
  155.         return list(tags)
  156.  
  157.     
  158.     def rename(self, song, newname, changed = None):
  159.         """Rename the song in all libraries it belongs to.
  160.  
  161.         The 'changed' signal will fire for any library the song is in
  162.         except if a set() is passed as changed.
  163.         """
  164.         re_add = []
  165.         print_d('Renaming %r to %r' % (song.key, newname), self)
  166.         for library in self.libraries.itervalues():
  167.             
  168.             try:
  169.                 del library._contents[song.key]
  170.             except KeyError:
  171.                 continue
  172.  
  173.             re_add.append(library)
  174.         
  175.         song.rename(newname)
  176.         for library in re_add:
  177.             library._contents[song.key] = song
  178.             if changed is None:
  179.                 library._changed(set([
  180.                     song]))
  181.                 continue
  182.             print_d('Delaying changed signal for %r.' % library, self)
  183.             changed.add(song)
  184.         
  185.  
  186.     
  187.     def reload(self, item, changed = None, removed = None):
  188.         '''Reload a song (for all libraries), possibly noting its status.
  189.  
  190.         If sets are given, it assumes the caller will handle signals,
  191.         and only updates the sets. Otherwise, it handles signals
  192.         itself. It *always* handles library contents, so do not
  193.         try to remove (again) a song that appears in the removed set.
  194.         '''
  195.         had_item = []
  196.         print_d('Reloading %r' % item.key, self)
  197.         for library in self.libraries.itervalues():
  198.             
  199.             try:
  200.                 del library._contents[item.key]
  201.             except KeyError:
  202.                 continue
  203.  
  204.             had_item.append(library)
  205.         
  206.         
  207.         try:
  208.             library = had_item[0]
  209.         except IndexError:
  210.             return None
  211.  
  212.         (was_changed, was_removed) = library._load_item(item, force = True)
  213.         if was_changed:
  214.             pass
  215.         if not not was_removed:
  216.             raise AssertionError
  217.         if None:
  218.             if removed is None:
  219.                 for library in had_item:
  220.                     library.emit('removed', set([
  221.                         item]))
  222.                 
  223.             else:
  224.                 removed.add(item)
  225.         elif was_changed:
  226.             for library in had_item:
  227.                 library._contents[item.key] = item
  228.             
  229.             if changed is None:
  230.                 for library in had_item:
  231.                     library.emit('changed', set([
  232.                         item]))
  233.                 
  234.             else:
  235.                 changed.add(item)
  236.  
  237.  
  238.